package masterjava.common.settings;

import masterjava.common.store.IPropertiesStore;
import masterjava.common.store.Property;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * User: GKislin
 * Date: 31-Jan-2011
 *
 * Factory for similar properties, based on IPropertiesStore
 * e.g. collection of hosts, ulrs, defined in one properties file
 */
public class MapProps<T> {

    private final Map<String, Property<T>> map = new HashMap<String, Property<T>>();
    private PropsFactory propsFactory;

    public MapProps(IPropertiesStore propStore, Class<T> clazz) {
        propsFactory = new PropsFactory(propStore);
        Iterator keyIterator = propStore.getKeysIterator();
        while (keyIterator.hasNext()) {
            String key = (String) keyIterator.next();
            Property<T> prop = createPropItem(key, clazz);
            map.put(key, prop);
        }
    }

    protected Property<T> createPropItem(String key, Class<T> clazz) {
        return propsFactory.create(key, clazz);
    }

    public Map<String, Property<T>> getMap() {
        return map;
    }

    public Property<T> get(String key) {
        return map.get(key);
    }

    public String getAsString(String key) {
        return get(key).getValue().toString();
    }
}
